package app.fugema.shop.util.passwardview;

import com.base.common.utils.LogUtil;
import com.jungly.gridpasswordview.GridPasswordView;

import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import app.fugema.shop.R;

//也可继承design中的BottomSheetDialogFragment       DialogFragment
public class PassWordAgainFragment extends DialogFragment
{
    //修改设置 2次验证密码
    //验证密码时 键盘不是乱序

    private PasswordAgainView mPassword;

    private FinishInputPassword  finishAction;

    private OnPasswordDiffErrorListener  diffPwdAction;

    private PasswordDialogCloseLinstener closeLinstener;

    public static PassWordAgainFragment newInstace(FinishInputPassword  finishActionInf, OnPasswordDiffErrorListener diffErrorListener, PasswordDialogCloseLinstener mCloseLinstener)
    {
        PassWordAgainFragment passWordFragment = new PassWordAgainFragment();
        passWordFragment.finishAction = finishActionInf;
        passWordFragment.diffPwdAction = diffErrorListener;
        passWordFragment.closeLinstener = mCloseLinstener;
        return passWordFragment;
    }

    public void setFinishAction(FinishInputPassword finishAction) {
        this.finishAction = finishAction;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    //继承BottomSheetDialogFragment时onStart()可注释掉
    @Override
    public void onStart() {
        super.onStart();

        Window win = getDialog().getWindow();
        win.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.translucent)));
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        layoutParams.width = (int) (dm.widthPixels);
        win.setAttributes(layoutParams);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.dialog_password_again, container);
        init(view);
        return view;
    }

    private void init(View view)
    {
        mPassword = (PasswordAgainView) view.findViewById(R.id.view_password);
        mPassword.getCloseImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        mPassword.getTitleTextView().setText("设置密码");

        mPassword.getPswView().setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {

                LogUtil.e("getPswView_onInputFinish -> psw = " + psw  );
                //密码输入完
                if (mPassword.getFirstPassword().length() == 6 && mPassword.getPassword().length() == 6)
                {
                        String firstPwd = mPassword.getFirstPassword().trim();
                        String pwd = mPassword.getPassword().trim();
                        LogUtil.e("getPswView_onInputFinish -> firstPwd = " + firstPwd + "  pwd = " + pwd  );
                        if( firstPwd.equals(pwd))
                        {
                            LogUtil.e("getPswView_onInputFinish -> firstPwd == pwd" );
                            dismiss();
                            finishAction.finishPassword(pwd);
                        }
                        else
                        {
                            LogUtil.e("getPswView_onInputFinish -> firstPwd != pwd" );
                            //2次输入的密码不一样
                            if( diffPwdAction != null )
                            {
                                diffPwdAction.onPasswordDiff();
                            }
                        }
                }
            }

            @Override
            public void onInputFinish(String psw)
            {}
        });

        // 2次验证密码的 第一次密码
    }

    //2次 密码 不一样
    public interface OnPasswordDiffErrorListener
    {
        void onPasswordDiff();
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog)
    {
        super.onDismiss(dialog);
        if( closeLinstener != null )
        {
            closeLinstener.onDialogDismiss();
        }
    }
}
